package com.moxiao.yfjh.module.order.common;

import com.moxiao.yfjh.framework.common.util.date.DateUtils;
import com.moxiao.yfjh.module.blind.controller.admin.member.vo.BlindMemberUserUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeOrder.vo.BlindTradeOrderCreateReqVO;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeRefund.vo.BlindTradeRefundCreateReqVO;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeOrder.BlindTradeOrderDO;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeRefund.BlindTradeRefundDO;
import com.moxiao.yfjh.module.order.tradeRefund.blindTradeOrder.BlindTradeOrderService;
import com.moxiao.yfjh.module.order.tradeRefund.blindTradeRefund.BlindTradeRefundService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * 编号统一工具类
 **/
@Service
public class GenNoCommonUtil {

    private static final String  PREFIX_MEMBER_ORDER = "HYDD";
    private static final String PREFIX_MEMBER_USER = "JFHY";
    private static final String PREFIX_MEMBER_DISTRIBUTION = "FXYJ";

    private static final String PREFIX_REFUND = "RF";
    @Resource
    private BlindTradeOrderService blindTradeOrderService;

    @Resource
    private BlindMemberUserService memberUserService;

    @Resource
    private BlindTradeRefundService refundService;

    /**
     * 增加 userNo(用户编号)字段
     * @param createReqVO
     */
    public void addOrderNo(BlindTradeOrderCreateReqVO createReqVO){
        BlindTradeOrderDO blindTradeOrderDO = blindTradeOrderService.getTradeOrderTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(blindTradeOrderDO)){
            String[] user_no = blindTradeOrderDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MEMBER_ORDER + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_MEMBER_ORDER + "_" + dateTime + "_" + "0001";
            Integer num = blindTradeOrderService.getTradeOrderNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }

    /**
     * 增加 userNo(用户编号)字段
     * @param createReqVO
     */
    public void addUserNo(BlindMemberUserUpdateReqVO updateReqVO){
        BlindMemberUserDO blindMemberUserDO = memberUserService.getMemberUserTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(blindMemberUserDO)){
            String[] user_no = blindMemberUserDO.getUserNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                updateReqVO.setNo(PREFIX_MEMBER_USER + "_" + dateTime + "_" + no);
            }
        }else {
            String user_no = PREFIX_MEMBER_USER + "_" + dateTime + "_" + "0001";
            Integer num = memberUserService.getMemberUserNo(user_no);
            if (num == 0) {
                updateReqVO.setNo(user_no);
            }
        }
    }


    /**
     * 增加 userNo(用户编号)字段
     *
     * @param createReqVO
     */
    public void addRefundNo(BlindTradeRefundCreateReqVO createReqVO) {
        BlindTradeRefundDO refundDO = refundService.getTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(refundDO)) {
            String[] user_no = refundDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_REFUND + "_" + dateTime + "_" + no);
            }
        } else {
            String user_no = PREFIX_REFUND + "_" + dateTime + "_" + "0001";
            Integer num = refundService.getNo(user_no);
            if (num == 0) {
                createReqVO.setNo(user_no);
            }
        }
    }
}
